// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBInventoryHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.DataModel
{
    #region Usings

    using System;
    using System.Linq;
    using System.Reflection;

    #endregion

    /// <summary>
    /// The db inventory helpers.
    /// </summary>
    public class DBInventoryHelpers
    {
        #region Constants and Fields

        /// <summary>
        /// The Identity.
        /// </summary>
        private static readonly Identity Identity = Identity.Create(MethodBase.GetCurrentMethod());

        #endregion

        #region Public Methods

        /// <summary>
        /// The get db iventory.
        /// </summary>
        /// <param name="dbBackOfficeId">
        /// The db order service id.
        /// </param>
        /// <param name="bookingPath">
        /// The booking path.
        /// </param>
        /// <returns>
        /// </returns>
        public static Status<DBInventory> GetDBInventory(Guid dbBackOfficeId, string bookingPath)
        {
            var nodeId = Guid.Empty; // TODO split booking path -> node

            var inventories = from i in Singleton<DBForge>.Instance.Entities.DBInventory
                              where i.DBBackOffice.ID == dbBackOfficeId
                              where i.DBExecutionContextINodeID == nodeId
                              select i;

            if (inventories.Count() == 1)
            {
                return Status.Success<DBInventory>(Identity, inventories.First());
            }
            else if (inventories.Count() > 1)
            {
                return Status.Failure<DBInventory>(Identity, "There are more than one inventories");
            }

            return Status.Failure<DBInventory>(Identity, "Not found");
        }

        #endregion
    }
}